$(function (){
    let app = new Vue({
        el: "#app",
        data: {
            formData: {},//表单数据
            sysTitle: "固定资源管理系统",
            userID: "", //用户ID
            userName: "", //用户姓名
            userImageSrc: "", //用户头像
            userClass: "",  //用户类型
            userDepartment: "", //用户类型
            userType: "",
            backImageSrc: "images/common/back.jpg",
            oldPassword: "",
            newPassword: "",
            celPassword: "",
            rules: {//校验规则
                password: [{ required: true, message: '用户密码为必填项', trigger: 'blur' }],
            },
            subList1: [
                {href:"properties.html", li: " 资源管理"},
                {href:"departments.html", li: " 部门管理"},
                {href:"employees.html", li: " 员工管理"}
            ],
            subList2: [
                {href:"infos.html", li: " 个人信息"},
                {href:"passwords.html", li: " 密码修改"},
                {href:"util/login.html", li: " 注销"}
            ]
        },
        created(){
            this.getIdAndName();
        },
        methods: {
            getIdAndName(){
                if(sessionStorage.getItem("userID") === null)
                {
                    location.href = "util/login.html";
                }else {
                    this.userID = sessionStorage.getItem("userID").toString();
                    this.userName = sessionStorage.getItem("userName").toString();
                    this.userImageSrc = "../" + sessionStorage.getItem("userImageSrc").toString();
                    this.userType = sessionStorage.getItem("userType").toString();
                    this.userClass = sessionStorage.getItem("userClass").toString();
                    this.userDepartment = sessionStorage.getItem("userDepartment").toString();

                    //获取用户信息
                    let userType = this.userType;
                    if(userType === "0")
                    {
                        axios.get("http://localhost:16/mangers/"+this.userID).then((res)=>{
                            this.formData = res.data.data;
                        });
                    }else {
                        axios.get("http://localhost:16/employees/"+this.userID).then((res)=>{
                            this.formData = res.data.data;
                        });
                    }
                }
            },
            clearPassword()
            {
                this.oldPassword = "";
                this.newPassword = "";
                this.celPassword = "";
            },
            handleCancel() {
                this.clearPassword();
                this.$message.error("操作已取消，已为你清空！");
            },
            handleUpdate() {
                let pwd = this.oldPassword;
                let newPwd = this.newPassword;
                let celPwd = this.celPassword;
                if(pwd === "")
                {
                    this.$message.error("旧密码不能为空！");
                    return;
                }
                if(newPwd === "")
                {
                    this.$message.error("新的密码不能为空！");
                    inputs.eq(1).focus();
                    return;
                }
                if(celPwd === "")
                {
                    this.$message.error("确定密码不能为空！");
                    return;
                }
                if(pwd === this.formData.password)
                {
                    if(newPwd.length < 8 || celPwd.length < 8)
                    {
                        this.$message.error("新的密码位数不能小于8位！");
                        return;
                    }
                    if(newPwd === celPwd)
                    {
                        this.formData.password = newPwd;
                        this.changePassword();
                    }else {
                        this.$message.error("新的密码不一致！");
                    }

                }else {
                    this.$message.error("旧密码不正确，请重新输入！");
                    this.oldPassword = "";
                }

            },
            changePassword()
            {
                if(this.userType === "0")
                {
                    axios.put("http://localhost:16/mangers",this.formData).then((res)=>{
                        if(res.data.flag){
                            this.$message.success(res.data.msg);
                            this.clearPassword();
                        }else this.$message.error(res.data.msg);
                    })
                }else {
                    axios.put("http://localhost:16/employees",this.formData).then((res)=>{
                        if(res.data.flag){
                            this.$message.success(res.data.msg);
                            this.clearPassword();
                        }else this.$message.error(res.data.msg);
                    })
                }
            }

        }
    });

    /* lis.addClass("current");*/
    var left_nav = $(".left-nav");
    var left_lis = left_nav.children("li");
    var left_sub_nav = left_lis[1].children[1];
    $(left_sub_nav).children("li").eq(1).children("a").addClass("current");

});